﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.Enums;
using Votations.NSurvey.WebControls.UI;
using Votations.NSurvey.WebAdmin.UserControls;

namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Display the selection results of a survey
    /// </summary>
    public partial class ResultsReporting : PageBase
    {
        protected SurveyListControl SurveyList;
        protected System.Web.UI.WebControls.Label MessageLabel;
        protected System.Web.UI.WebControls.Literal ApplyFilter;
        public override string ActionCode
        {
            get
            {
                return "103";//报告
            }
        }
        protected void Page_Load(object sender, System.EventArgs e)
        {
            SetupSecurity();
            LocalizePage();

            if (!Page.IsPostBack)
            {
                SurveyList.SurveyId = SurveyId;
                SurveyList.BindDropDown();
                QuestionsDropDownList.DataSource = new Questions().GetQuestionListWithSelectableAnswers(SurveyId);
                QuestionsDropDownList.DataTextField = "QuestionText";
                QuestionsDropDownList.DataValueField = "QuestionID";
                QuestionsDropDownList.DataBind();
                QuestionsDropDownList.Items.Insert(0, new ListItem(GetPageResource("DisplayAllResultsMessage"), "0"));
                QuestionsDropDownList.Items.Insert(0, new ListItem(GetPageResource("SelectQuestionMessage"), "-1"));

                FilterDropDownList.DataSource = new Filters().GetFilters(SurveyId);
                FilterDropDownList.DataMember = "Filters";
                FilterDropDownList.DataTextField = "Description";
                FilterDropDownList.DataValueField = "FilterID";
                FilterDropDownList.DataBind();
                FilterDropDownList.Items.Insert(0,
                    new ListItem(GetPageResource("SelectFilterMessage"), "-1"));

                LayoutDropDownList.Items.Add(new ListItem(GetPageResource("ColumnChartOption"), "1"));
                LayoutDropDownList.Items.Add(new ListItem(GetPageResource("HTMLChartOption"), "2"));
                FilterEditorHyperLink.Text = GetPageResource("FilterEditorHyperLink");
                FilterEditorHyperLink.NavigateUrl = UINavigator.FilterEditor + "?SurveyId=" + SurveyId + "&menuindex=" + MenuIndex;
            }
        }

        private void SetupSecurity()
        {
            CheckRight(NSurveyRights.AccessReports, true);
            FilterEditorHyperLink.Visible = CheckRight(NSurveyRights.CreateResultsFilter, false);
        }

        private void LocalizePage()
        {
            SurveyResultsTitle.Text = GetPageResource("SurveyResultsTitle");
            QuestionsResultsDisplaylabel.Text = GetPageResource("QuestionsResultsDisplaylabel");
            ResultsLayoutLabel.Text = GetPageResource("ResultsLayoutLabel");
            ApplyFilterLabel.Text = GetPageResource("ApplyFilterLabel");
            ResultsOrderLabel.Text = GetPageResource("ResultsOrderLabel");
            if (!Page.IsPostBack)
            {
                ResultsOrderDropDownList.Items.Add(new ListItem(GetPageResource("AnswerOrderOption"), "ANS"));
                ResultsOrderDropDownList.Items.Add(new ListItem(GetPageResource("AscendingOrderOption"), "ASC"));
                ResultsOrderDropDownList.Items.Add(new ListItem(GetPageResource("DescendingOrderOption"), "DESC"));
            }
        }

        /// <summary>
        /// Get the current DB stats and build 
        /// the results tables
        /// </summary>
        private void BuildHtmlResults()
        {
            QuestionData questionsData = new Questions().GetQuestionListWithSelectableAnswers(SurveyId);
            ChartRepeater.Visible = false;
            SingleChartImage.Visible = false;

            Table resultsTable = new Table();
            resultsTable.CellSpacing = 10;
            TableCell resultsTableCell = new TableCell();
            TableRow resultsTableRow = new TableRow();

            resultsTable.CssClass = "resultsTable";
            foreach (QuestionData.QuestionsRow question in questionsData.Questions.Rows)
            {
                resultsTableCell = new TableCell();
                resultsTableRow = new TableRow();
                Style questionStyle = new Style();
                Style answerStyle = new Style();
                questionStyle.Font.Bold = true;
                questionStyle.Font.Size = FontUnit.XSmall;
                questionStyle.ForeColor = System.Drawing.Color.Black;
                //questionStyle.BackColor = System.Drawing.Color.FromArgb(225,238,241);
                answerStyle.Font.Size = FontUnit.XXSmall;
                ResultItem questionResults = new ResultItem();
                questionResults.BorderWidth = Unit.Pixel(1);
                questionResults.BorderColor = System.Drawing.Color.FromArgb(123, 123, 123);
                questionResults.BackColor = System.Drawing.Color.FromArgb(243, 243, 243);
                questionResults.Width = Unit.Pixel(600);
                questionResults.BarColor = "blueadmin";
                questionResults.QuestionStyle = questionStyle;
                questionResults.AnswerStyle = answerStyle;
                questionResults.FootStyle = answerStyle;
                questionResults.DataSource = new Questions().GetQuestionResults(question.QuestionId, int.Parse(FilterDropDownList.SelectedValue), ResultsOrderDropDownList.SelectedValue);
                questionResults.DataBind();
                resultsTableCell.Controls.Add(questionResults);

                resultsTableRow.Cells.Add(resultsTableCell);
                resultsTable.Rows.Add(resultsTableRow);
            }

            ResultsPlaceHolder.Controls.Add(resultsTable);
        }

        protected void QuestionsDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (QuestionsDropDownList.SelectedValue != "-1")
            {
                if (LayoutDropDownList.SelectedValue == "2")
                {
                    BuildHtmlResults();
                }
                else
                {
                    BuildChartColumnResults(int.Parse(QuestionsDropDownList.SelectedValue));
                }
            }

        }

        void BuildChartColumnResults(int questionId)
        {
            if (questionId == 0)
            {
                ChartRepeater.Visible = true;
                SingleChartImage.Visible = false;
                ChartRepeater.DataSource = new Questions().GetQuestionListWithSelectableAnswers(SurveyId);
                ChartRepeater.DataBind();
            }
            else
            {
                ChartRepeater.Visible = false;
                SingleChartImage.Visible = true;
                SingleChartImage.ImageUrl = Server.HtmlDecode("BarChartReport.aspx?questionid=" + questionId + "&filterid=" + FilterDropDownList.SelectedValue + "&SortOrder=" + ResultsOrderDropDownList.SelectedValue);
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void FilterDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (QuestionsDropDownList.SelectedValue != "-1")
            {
                if (LayoutDropDownList.SelectedValue == "2")
                {
                    BuildHtmlResults();
                }
                else
                {
                    BuildChartColumnResults(int.Parse(QuestionsDropDownList.SelectedValue));
                }
            }
        }

    }

}
